// pages/mine/mine.js
const app = getApp()
var base_url = app.globalData.url
var template = require('../../../template/template.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    width: "33%",
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    },
    xuejinumber: '1234567890',
    ifName: true,
    home: {
      "current": 0,
      "pagePath": "/pages/home/ptwords/ptwords",
      "iconPath": "/images/icons/icona_a.png",
      "text": "习字"
    },
    // photo: {
    //   "current": 0,
    //   "pagePath": "/pages/photo/goTest/goTest",
    //   "iconPath": "/images/icons/icon1.png",
    //   "text": "评测"
    // },
    mine: {
      "current": 0,
      "pagePath": "/pages/my/teacher/mine/mine",
      "iconPath": "/images/icons/icon4.png",
      "text": "我的"
    },
    find: {
      "current": 0,
      "pagePath": "/pages/find/find",
      "iconPath": "/images/icons/icon3.png",
      "text": "班级"
    },
    store: {
      "current": 0,
      "pagePath": "/pages/store/storeHome/storeHome",
      "iconPath": "/images/icons/store_1.png",
      "text": "助学"
    },
    display:'block'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // template.tabbar("tabBar", 1, this)  
    var that = this;
    wx.getStorage({
      key: 'auth',
      success: function (res) {
        if (res['data'] == 1) {
          that.setData({
            // width: "25%",
            display: 'block'
          })
        } else {

        }
      },
    }) 
    /**
     * 获取用户信息
     */
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          [avatarUrl]: res.userInfo.avatarUrl,
          [nickName]: res.userInfo.nickName,
        })
      }
    })

    wx.getStorage({
      key: 'teacher_id',
      success: function (res) {
        that.setData({
          teacher_id:res.data
        })
      },
    })

    wx.getStorage({
      key: 'teacher_id',
      success: function (res) {
        wx.request({
          url: base_url+'/getTeacherInfo/',
          method: 'POST',
          data: {
            teacher_id: res.data
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            that.setData({
              name: res.data['trueName']
            })
            that.setData({
              school: res.data['schoolName']
            })
          }
        })
      },
    })
    wx.getStorage({
      key: 'auth',
      success: function (res) {
        that.setData({
          auth: res.data
        })
      },
    })

    wx.getStorage({
      key: 'student_id',
      success: function (res) {
        that.setData({
          student_id: res.data
        })
      },
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tosettings: function () {
    var that = this;
    that.setData({
      ifName: false
    })
  },
  cancel: function () {
    var that = this;
    that.setData({
      ifName: true
    })
  },
  gotoLeads: function () {
    wx.navigateTo({
      url: '../../leads/leads',
      success: function (res) {
        console.log(res)
      },
      fail: function () {

      }
    })
  },
  gotoOptions: function () {
    wx.navigateTo({
      url: '../../options/options',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
  },
  gotoClassExercise: function () {
    var that = this
    wx.navigateTo({
      url: '../exercise/exercise',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
    // wx.request({
    //   url: 'http://127.0.0.1:8000/getAllexercise/',
    //   method: 'POST',
    //   data: {
    //     teacher_id: that.data.teacher_id
    //   },
    //   header: {
    //     "Content-Type": "application/x-www-form-urlencoded"
    //   },
    //   success: function (res) {
    //     if (res.data['flag'] == true) {
    //       console.log(res.data['exerciseDatas_lst'])
    //       wx.setStorage({
    //         key: 'exerciseDatas_lst',
    //         data: res.data['exerciseDatas_lst'],
    //         success: function () {
    //           wx.navigateTo({
    //             url: '../exercise/exercise',
    //             success: function (res) {
    //               console.log(res)
    //             },
    //             fail: function () {
    //             }
    //           })
    //         }
    //       })
    //     }
    //   }
    // })
    
  },
  gotoClassManage: function () {
    var that = this;
    wx.request({
      url: base_url+'/getClassInfo/',
      method: 'POST',
      data: {
        teacher_id: that.data.teacher_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data['flag'] == true) {
          console.log(res.data['classDatas_lst'])
          wx.setStorage({
            key: 'classDatas_lst',
            data: res.data['classDatas_lst'],
            success: function () {
              wx.navigateTo({
                url: '../classManage/classManage',
                success: function (res) {
                  console.log(res)
                },
                fail: function () {
                }
              })
            }
          })
        }
      }
    })
    
  },
  
  gotoHistories: function () {
    var that = this
    
    wx.request({
      url: base_url+'/getStudentHistory/',
      method: 'POST',
      data: {
        auth: that.data.auth,
        student_id: that.data.student_id,
        teacher_id: that.data.teacher_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data['flag'] == true) {
          wx.setStorage({
            key: 'histort_datas',
            data: res.data['lst'],
            success: function () {
              wx.navigateTo({
                url: '../../visitor/histories/histories',
                success: function (res) {
                }
              })
            },

          })
          // that.setData({
          //   histort_datas: res.data['lst']
          // })
        }
      }
    })
    console.log(that.data.histort_datas)
        
  },
})